/*
* ExaminationController.java
* Created on  2014-7-30 下午2:30
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-7-30       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.task.examination;

import com.genghis.core.autocomplete.service.AutoCompleteService;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.system.entity.DictValues;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.task.entity.ExaminationInfo;
import com.genghis.ptas.task.entity.LibraryTask;
import com.genghis.ptas.task.service.ExaminationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 类的描述信息
 *
 * @author gaoxinyu
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/task/examination/")
public class ExaminationController {
    @Autowired
    private ExaminationService examinationService;

    @Autowired
    private DictService dictService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private AutoCompleteService autoCompleteService;

    @RequestMapping(value = "listAllExaminationDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllExaminationDo() {
        return "backstage/task/examination/listAllExaminationDo";
    }

    @RequestMapping(value = "doExaminationDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String doExaminationDo(HttpServletRequest request, @RequestParam("id") Integer id)throws ServletException, IOException {
        ExaminationInfo examinationInfo = examinationService.findExaminationById(id);
        List<DictValues> dictValuesList = dictService.findAllDictValues(DictConstant.TASKTYPE);
        request.setAttribute("type", dictValuesList);
        request.setAttribute("examinationInfo", examinationInfo);
        return "frontstage/examination/doExaminationDo";
    }

    @RequestMapping(value = "listAllExamination", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllExamination(PageParameter pageParameter, ExaminationInfo examinationInfo) {
        return examinationService.findExaminationPageList(pageParameter, examinationInfo);
    }

    @RequestMapping(value = "addExaminationDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String addDictDefineDo(HttpServletRequest request) throws ServletException, IOException {
        List<DictValues> dictValuesList = dictService.findAllDictValues(DictConstant.TASKTYPE);
        request.setAttribute("type", dictValuesList);
        return "backstage/task/examination/addExaminationDo";
    }

    @RequestMapping(value = "analyzeExamination", method = RequestMethod.POST)
    @ResponseBody
    public ExaminationInfo analyzeExamination(ExaminationInfo examinationInfo) {
        return examinationService.analyzeExamination(examinationInfo);
    }

    @RequestMapping(value = "addExaminationTaskToCache", method = RequestMethod.POST)
    @ResponseBody
    public List<LibraryTask> addExaminationTaskToCache(@RequestParam("taskIds") String taskId) {
        return examinationService.addExaminationTaskToCache(taskId);
    }

    @RequestMapping(value = "displayExaminationInfoToCache", method = RequestMethod.POST)
    @ResponseBody
    public List<LibraryTask> displayExaminationInfoToCache(@RequestParam("examinationInfoId") Integer id) {
        return examinationService.findTaskByExaminationInfoId(id);
    }

    @RequestMapping(value = "addExamination", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void addExamination(HttpServletRequest request, @RequestParam("taskIds") String taskId, @RequestParam("type") Integer type) throws UnsupportedEncodingException {
        examinationService.addExamination(new String(request.getParameter("examinationName").getBytes("ISO8859-1"), "UTF-8"), taskId, type);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "editExaminationDo", method = {RequestMethod.GET, RequestMethod.POST})
    public String editExaminationDo(HttpServletRequest request, @RequestParam("id") Integer id) {
        ExaminationInfo examinationInfo = examinationService.findExaminationById(id);
        List<DictValues> dictValuesList = dictService.findAllDictValues(DictConstant.TASKTYPE);
        request.setAttribute("type", dictValuesList);
        request.setAttribute("examinationInfo", examinationInfo);
        return "backstage/task/examination/editExaminationDo";
    }

    @RequestMapping(value = "editExamination", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void editExamination(HttpServletRequest request, @RequestParam("taskIds") String taskId, @RequestParam("examinationId") Integer examinationId, @RequestParam("type") Integer type) throws UnsupportedEncodingException {
        examinationService.deleteExaminationById(examinationId);
        examinationService.addExamination(new String(request.getParameter("examinationName").getBytes("ISO8859-1"), "UTF-8"), taskId, type);
        autoCompleteService.initAutoComplete();
    }

    @RequestMapping(value = "deleteExamination", method = RequestMethod.DELETE)
    @ResponseBody
    public void deleteExamination(ExaminationInfo examinationInfo) {
        examinationService.deleteExaminationById(examinationInfo.getId());
        autoCompleteService.initAutoComplete();
    }
}



